import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../Layout/layout.vue'
import Login from '../views/Login'
import local from '../utils/local'

Vue.use(VueRouter)

const routes = [
  // 后台主页面
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      // 首页
      {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName: "home" */ '../views/Home')
      },

      // 订单管理
      {
        path: '/orders',
        meta: { name: '订单管理' },
        component: () =>
          import(/* webpackChunkName: "home" */ '../views/Orders')
      },

      // 商品管理
      {
        path: '/goods',
        meta: { name: '商品管理' },
        component: () =>
          import(/* webpackChunkName: "goods" */ '../views/Goods'),
        redirect: '/goods/list',
        children: [
          {
            path: '/goods/list',
            meta: { name: '商品列表' },
            component: () =>
              import(
                /* webpackChunkName: "goods" */ '../views/Goods/goodsList.vue'
              )
          },
          {
            path: '/goods/add',
            meta: { name: '商品添加' },
            component: () =>
              import(
                /* webpackChunkName: "goods" */ '../views/Goods/goodsAdd.vue'
              )
          },
          {
            path: '/goods/cate',
            meta: { name: '商品分类' },
            component: () =>
              import(
                /* webpackChunkName: "goods" */ '../views/Goods/goodsCate.vue'
              )
          }
        ]
      },

      // 店铺管理
      {
        path: '/shop',
        meta: { name: '店铺管理' },
        component: () => import(/* webpackChunkName: "home" */ '../views/Shop')
      },

      // 账号管理
      {
        path: '/account',
        meta: { name: '账号管理' },
        component: () =>
          import(/* webpackChunkName: "account" */ '../views/Account'),
        redirect: '/account/list',
        children: [
          {
            path: '/account/list',
            meta: { name: '账号列表' },
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/accountList.vue'
              )
          },
          {
            path: '/account/add',
            meta: { name: '账号添加' },
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/accountAdd.vue'
              )
          },
          {
            path: '/account/edit',
            meta: { name: '修改密码' },
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/accountEdit.vue'
              )
          },
          {
            path: '/account/mine',
            name: 'mine',
            meta: { name: '个人中心' },
            component: () =>
              import(
                /* webpackChunkName: "account" */ '../views/Account/Mine.vue'
              )
          }
        ]
      },

      // 销售管理
      {
        path: '/total',
        meta: { name: '销售统计' },
        redirect: '/total/order',
        component: () =>
          import(/* webpackChunkName: "total" */ '../views/Total'),
        children: [
          {
            path: '/total/goods',
            meta: { name: '商品统计' },
            component: () =>
              import(
                /* webpackChunkName: "total" */ '../views/Total/totalGoods.vue'
              )
          },
          {
            path: '/total/order',
            meta: { name: '订单统计' },
            component: () =>
              import(
                /* webpackChunkName: "total" */ '../views/Total/totalOrder.vue'
              )
          }
        ]
      }
    ]
  },
  // 登录页面
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  // 重定向404页面
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/404',
    name: 'error',
    component: () => import(/* webpackChunkName: "error" */ '../views/Error')
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
